package com.jcark.utilscode;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import androidx.annotation.NonNull;

/**
 * Description: 支付宝工具类<br>
 *
 * @author King.Zi<br>
 * @date 2022/6/18<br>
 * Email: 78688689@qq.com
 */
public class AliPlayUtils {
    private AliPlayUtils() {
    }

    /**
     * 是否为支付宝支付
     *
     * @param url 支付请求URL
     * @return 是否为支付宝支付
     */
    public static boolean isAliPay(@NonNull String url) {
        return url.startsWith("alipays:") || url.startsWith("alipay");
    }

    /**
     * 调用支付宝
     *
     * @param activity 上下文
     * @param url      url地址
     */
    public static void callAliPay(@NonNull Activity activity, String url) {
        if (isAliPayInstalled(activity)) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            activity.startActivity(intent);
            activity.finish();
        }
    }

    /**
     * 判断是否安装支付宝应用
     *
     * @param context 上下文档
     * @return 是否安装支付宝应用
     */
    public static boolean isAliPayInstalled(@NonNull Context context) {
        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName = intent.resolveActivity(context.getPackageManager());
        return componentName != null;
    }
}